package com.stack.knowyouweb.controller;


import com.google.gson.Gson;

import com.stack.knowyoubase.constant.GlobalConst;
import com.stack.knowyoubean.bean.KnowLedge;
import com.stack.knowyoubean.bean.RepJson;
import com.stack.knowyouservice.service.KnowLedgeService;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * 知识点服务层
 *
 * @author stack
 */
@RestController
@Scope("singleton")
@RequestMapping("/api")
public class KnowLedgeController {
    /**
     * 注入基于proxy的接口代理对象
     */
    @Resource
    private KnowLedgeService knowLedgeService;
    @Resource
    private Gson gson;

    @GetMapping("/knowledge")
    public String knowLedgeSearch(String kname, String kclass, String kchap, Integer page, Integer limit, HttpSession session, Authentication authentication) {
        RepJson<List<KnowLedge>> repJson = new RepJson<>();
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setMsg("没有选择课程");
        } else {
            HashMap<String, Object> knowLedgeMap = knowLedgeService.knowLedgeSearch(sid, kname, kclass, kchap, page, limit, authentication);
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setMsg("查询成功");
            repJson.setCount((Integer) knowLedgeMap.get("count"));
            repJson.setData((List<KnowLedge>) knowLedgeMap.get("knowLedgeList"));
        }
        return gson.toJson(repJson);
    }

    @PostMapping("/knowledge")
    public String knowLedgeAdd(KnowLedge knowLedge, HttpSession session) {
        RepJson<String> repJson = new RepJson<>();
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
        } else {
            knowLedge.setSid(sid);
            boolean b = knowLedgeService.knowLedgeAdd(knowLedge);
            if (b) {
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.SUCCESS_TYPE);
                repJson.setMsg("添加知识点成功");
            } else {
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.ERROR_TYPE);
                repJson.setMsg("添加知识点失败");
            }
        }
        return gson.toJson(repJson);
    }

    @PutMapping("/knowledge")
    public String knowledgeUpdate(KnowLedge knowLedge, HttpSession session) {
        RepJson<String> repJson = new RepJson<>();
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
        } else {
            knowLedge.setSid(sid);
            boolean b = knowLedgeService.knowLedgeUpdate(knowLedge, sid);
            if (b) {
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.SUCCESS_TYPE);
                repJson.setMsg("更新知识点成功");
            } else {
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.ERROR_TYPE);
                repJson.setMsg("更新知识点失败");
            }
        }
        return gson.toJson(repJson);
    }

    @DeleteMapping("/knowledge")
    public String knowledgeDelete(Integer kid, HttpSession session) {
        RepJson<String> repJson = new RepJson<>();
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
        } else {
            boolean b = knowLedgeService.knowLedgeDelete(kid, sid);
            if (b) {
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.SUCCESS_TYPE);
                repJson.setMsg("删除知识点成功");
            } else {
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.ERROR_TYPE);
                repJson.setMsg("删除知识点失败");
            }
        }
        return gson.toJson(repJson);
    }

}
